<?php


namespace app\common\model;

use catchAdmin\permissions\model\DataRangScopeTrait;
use catchAdmin\permissions\model\Department;
use catchAdmin\permissions\model\Roles;
use catcher\base\CatchModel;
use catcher\Utils;


class BaseModel extends CatchModel
{
    use DataRangScopeTrait;

    protected $connection = "mongodb";
    protected $pageLimit = 15;
    protected $page = 1;

    public function __construct(array $data = [])
    {
        parent::__construct($data);
        $this->pageLimit = request()->param("limit", 15, "int");
        $this->page = request()->param("current", 1, "int");
    }


    public function mongoSearch(array $field = [], array $searchField = [], array $exceptFiled = [])
    {

        $param = request()->except(['limit', 'page']);

        $searchFiledArr = [];
        foreach ($param as $k => $v) {

            if ($v !== "") {
                if (!empty($searchField)) {
                    if (!in_array($k, $searchField, true)) {
                        continue;
                    }
                }
                if (in_array($k, $exceptFiled, true)) {
                    continue;
                }
                $searchFiledArr[] = $k;


            }
        }

        $result = $this->getModel();
        if (!empty($searchFiledArr)) {
            $result = ($this->getModel())->withSearch($searchFiledArr, $param);
        }
        if (!empty($field)) {
            return $result->field($field);
        }
        return $result;
    }

    /**
     * 根据登录用户获取对应的用户列表
     * @return array
     */
    public function getUserIds()
    {
        if (!Utils::isSuperAdmin()) {

            return $this->getDepartmentUserIdsBy([]);
        }
        return [];
    }

    /**
     * 获取部门IDs
     *
     * @param $roles
     * @return array
     * @author JaguarJack <njphper@gmail.com>
     * @date 2020/6/6
     */
    public function getDeptListByUser($roles)
    {
        $deptIds = [];

        $isAll = false;

        $user = request()->user();
        $customDeptId = (int)request()->header("custom_dept_id", "");

        if (!empty($customDeptId) && !empty($user["other_dept"])) {
            //判断部门id是否再user表中
            $deptList = (new \catchAdmin\permissions\model\Department())->getDeptByIds($user["other_dept"]);

            if (!empty($deptList)) {
                $deptIds = array_column($deptList, "id");
                $deptName = array_column($deptList, "department_name", "id");


                if (in_array($customDeptId, $deptIds, true)) {

                    $user->department_id = $customDeptId;
                    $user->department_name = $deptName[$customDeptId];


                }
            }

        }

        if (empty($roles)) {
            $roles = $user->getRoles();
        }

        foreach ($roles as $role) {
            switch ($role->data_range) {
                case Roles::ALL_DATA:
                    $deptIds = [];
                    $isAll = true;
                    break;
                case Roles::SELF_CHOOSE:
                    $deptIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));

                    break;
                case Roles::DEPARTMENT_DATA:
                    $deptIds[] = $user->department_id;
                    break;
                case Roles::DEPARTMENT_DOWN_DATA:
                    // 查一下下级部门
                    $deptIds = Department::getChildrenDepartmentIds($user->department_id);

                    break;
                case Roles::SELF_DATA:
                    $deptIds[] = $user->department_id;
                    break;

            }

            // 如果有全部数据 直接跳出
            if ($isAll) {
                break;
            }
        }

        return $deptIds;
    }


    /**
     * 根据登录用户获取对应权限
     * @return array
     */
    public function getUserJurisdiction($type)
    {
        if (!Utils::isSuperAdmin()) {
            return $this->getJurisdiction([], $type);
        }
        return [];
    }

    /**
     * 获取对应数据权限
     *
     * @param $roles
     * @param $type
     * @author YKX <yu734612787@qq.com>
     * @date 2022/12/23
     */
    /*public function getJurisdiction($roles)
    {
        $userIds = [];

        $isAll = false;

        $user = request()->user();
        $customDeptId = (int)request()->header("custom-dept-id");

        if (!empty($customDeptId) && !empty($user["other_dept"])) {
            //判断部门id是否再user表中
            $deptList = (new \catchAdmin\permissions\model\Department())->getDeptByIds($user["other_dept"]);

            if (!empty($deptList)) {
                $deptIds = array_column($deptList, "id");
                $deptName = array_column($deptList, "department_name", "id");
                if (in_array($customDeptId, $deptIds, true)) {
                    $user->department_id = $customDeptId;
                    $user->department_name = $deptName[$customDeptId];
                    $user->dept_name = $deptName[$customDeptId];
                }
            }

        }
        if (empty($roles)) {
            $roles = $user->getRoles();
        }

        foreach ($roles as $role) {
            switch ($role->data_range) {
                case Roles::ALL_DATA:
                    $isAll = true;
                    break;
                case Roles::SELF_CHOOSE:
                    $departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));

                    $doctor_code = $this->getUserIdsByDepartmentId($departmentIds, 'doctor_code');
                    $doctor_codes = array_map(function ($v) {
                        return (string)$v;
                    }, $doctor_code);
                    $userIds[] = ["inchargeDoctorNo", 'in', $doctor_codes];
                    break;
                case Roles::SELF_DATA:
                    $userIds[] = ["inchargeDoctorNo", '=', (string)$user->doctor_code];
                    break;
                case Roles::DEPARTMENT_DATA:
                    $doctor_code = $this->getUserIdsByDepartmentId([$user->department_id], 'doctor_code');
                    $doctor_codes = array_map(function ($v) {
                        return (string)$v;
                    }, $doctor_code);
                    $userIds[] = ["inchargeDoctorNo", 'in', $doctor_codes];
                    break;
                case Roles::DEPARTMENT_DOWN_DATA:
                    // 查一下下级部门
                    $departmentIds = Department::getChildrenDepartmentIds($user->department_id);
                    $doctor_code = $this->getUserIdsByDepartmentId($departmentIds, 'doctor_code');

                    $doctor_codes = array_map(function ($v) {
                        return (string)$v;
                    }, $doctor_code);
                    $userIds[] = ["inchargeDoctorNo", 'in', $doctor_codes];
                    break;
                default:
                    break;
            }

            // 如果有全部数据 直接跳出
            if ($isAll) {
                break;
            }
        }
        return $userIds;
    }*/
    
    public function getJurisdiction($roles, $type = '')
    {
        $userIds = [];

        $isAll = false;

        $user = request()->user();

        $customDeptId = (int)request()->header("custom-dept-id");

        if (!empty($customDeptId) && !empty($user["other_dept"])) {
            //判断部门id是否再user表中
            $deptList = (new \catchAdmin\permissions\model\Department())->getDeptByIds($user["other_dept"]);

            if (!empty($deptList)) {
                $deptIds = array_column($deptList, "id");
                $deptName = array_column($deptList, "department_name", "id");
                if (in_array($customDeptId, $deptIds, true)) {
                    $user->department_id = $customDeptId;
                    $user->department_name = $deptName[$customDeptId];
                    $user->dept_name = $deptName[$customDeptId];
                }
            }
        }
        if (empty($roles)) {
            $roles = $user->getRoles();
        }
        foreach ($roles as $role) {
            switch ($role->data_range) {
                case Roles::ALL_DATA:
                    $isAll = true;
                    break;
                case Roles::SELF_CHOOSE:
                    $departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));
                    $doctor_code = $this->getUserIdsByDepartmentId($departmentIds, 'doctor_code');
                    $doctor_codes = array_map(function ($v) {
                        return (string)$v;
                    }, $doctor_code);
                    if ($type == 1) {
                        $userIds[] = ["outList.attendingDoctorNo", "in", $doctor_codes];
                    } elseif ($type == 2) {
                        $userIds[] = ["inList.inchargeDoctorNo", "in", $doctor_codes];
                    } elseif ($type == 3) {
                        $userIds[] = ["hoList.inchargeDoctorNo", "in", $doctor_codes];
                    } else {
                        $userIds[] = ["outList.attendingDoctorNo|inList.inchargeDoctorNo|hoList.inchargeDoctorNo", 'in', $doctor_codes];
                    }
                    break;
                case Roles::SELF_DATA:
                    if ($type == 1) {
                        $userIds[] = ["outList.attendingDoctorNo", '=', (string)$user->doctor_code];
                    } elseif ($type == 2) {
                        $userIds[] = ["inList.inchargeDoctorNo", '=', (string)$user->doctor_code];
                    } elseif ($type == 3) {
                        $userIds[] = ["hoList.inchargeDoctorNo", '=', (string)$user->doctor_code];
                    } else {
                        $userIds[] = ["outList.attendingDoctorNo|inList.inchargeDoctorNo|hoList.inchargeDoctorNo", '=', (string)$user->doctor_code];
                    }
                    break;
                case Roles::DEPARTMENT_DATA:
                    $doctor_code = $this->getUserIdsByDepartmentId([$user->department_id], 'doctor_code');
                    $doctor_codes = array_map(function ($v) {
                        return (string)$v;
                    }, $doctor_code);
                    if ($type == 1) {
                        $userIds[] = ["outList.attendingDoctorNo", "in", $doctor_codes];
                    } elseif ($type == 2) {
                        $userIds[] = ["inList.inchargeDoctorNo", "in", $doctor_codes];
                    } elseif ($type == 3) {
                        $userIds[] = ["hoList.inchargeDoctorNo", "in", $doctor_codes];
                    } else {
                        $userIds[] = ["outList.attendingDoctorNo|inList.inchargeDoctorNo|hoList.inchargeDoctorNo", 'in', $doctor_codes];
                    }
                    break;
                case Roles::DEPARTMENT_DOWN_DATA:
                    // 查一下下级部门
                    $departmentIds = Department::getChildrenDepartmentIds($user->department_id);
                    $doctor_code = $this->getUserIdsByDepartmentId($departmentIds, 'doctor_code');
                    $doctor_codes = array_map(function ($v) {
                        return (string)$v;
                    }, $doctor_code);

                    if ($type == 1) {
                        $userIds[] = ["outList.attendingDoctorNo", "in", $doctor_codes];
                    } elseif ($type == 2) {
                        $userIds[] = ["inList.inchargeDoctorNo", "in", $doctor_codes];
                    } elseif ($type == 3) {
                        $userIds[] = ["hoList.inchargeDoctorNo", "in", $doctor_codes];
                    } else {
                        $userIds[] = ["outList.attendingDoctorNo|inList.inchargeDoctorNo|hoList.inchargeDoctorNo", 'in', $doctor_codes];
                    }
                    break;
                default:
                    break;
            }

            // 如果有全部数据 直接跳出
            if ($isAll) {
                break;
            }
        }
        return $userIds;
    }


}